#include "add.h"
#include "ui_add.h"

add::add(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::add)
{
    ui->setupUi(this);
    setFixedSize(400, 300);
}

add::~add()
{
    delete ui;
}

void add::set_type(int a)
{
    type=a;
}

void add::on_pushButton_clicked()  //当add按钮被点击时触发
{
    QString no,college,name,sql;
    no=ui->text_no->toPlainText();            //获取管理员输入的账号
    college=ui->text_college->toPlainText();  //获取学院
    name=ui->text_name->toPlainText();        //获取名称
    QSqlQuery query;
    if(type==0)  //在student表插入
    {
        sql="insert into student values('"+no+"','"+name+"','"+college+"','"+no+"')";  //插入输入的值
        query.exec(sql);
    }
    else if(type==1)  //在teacher表插入
    {
        sql="insert into teacher values('"+no+"','"+name+"','"+college+"','"+no+"',0)";
        query.exec(sql);
    }
    else if(type==2)  //在course表插入
    {
        sql="insert into course values('"+no+"','"+name+"','"+college+"',30,30)";
        query.exec(sql);
    }
    QMessageBox::about(NULL,"tip","insert success!");  //提示框：插入成功
    ui->text_college->setText("");
    ui->text_name->setText("");
    ui->text_no->setText("");       //将三个文本框清空
}

void add::on_pushButton_2_clicked() //当back按钮点击时触发
{
    this->close();
}
